package cn.highedu.spring.v2;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import java.util.Map;
import java.util.Set;

@ComponentScan("cn.highedu.spring.v2")
public class SpringTest {
    private static ApplicationContext context;
    @BeforeEach
    public void createContext() {
        context = new AnnotationConfigApplicationContext(SpringTest.class);
    }
    @Test
    public void test1() {
        Aaa a = context.getBean("b", Aaa.class);
        a.run();
    }

    @Test
    public void test2() {
        Aaa a = context.getBean("c", Aaa.class);
        a.run();
    }

    @Test
    public void test3() {
        Aaa a = context.getBean(Aaa.class);
        a.run();
    }

    @Test
    public void test4() {
        // getBeansOfType得到实现了某一接口的所有类的对象 返回一个Map集合
        // 集合的key表示被管理对象的标识(id) 集合的value表示被管理的对象
        Map<String, Aaa> beansOfType = context.getBeansOfType(Aaa.class);
        Set<String> keys = beansOfType.keySet();
        keys.forEach(System.out::println);
    }
}
